#include "atomic_0817_event_trigger_detection_service.h"

int detectEvent(EventDetectionInput *eventInput, EventDetectionOutput *eventOutput) {
    // 检查输入输出指针有效性
    if (eventInput == nullptr || eventOutput == nullptr) {
        return ATOMIC_SERVICE_ERROR;
    }

    // 初始化输出参数
    eventOutput->event_triggered = false;
    eventOutput->event_type = "";
    eventOutput->severity_level = 0;
    eventOutput->return_type = ATOMIC_SERVICE_SUCCESS;
    eventOutput->current_phase = 0;
    eventOutput->trigger_signal = 0;
    eventOutput->config_id = 0;

    // 定义检测阈值
    const int HIGH_CPU_THRESHOLD = 80;    // CPU使用率高阈值(%)
    const int HIGH_MEMORY_THRESHOLD = 90; // 内存使用率高阈值(%)

    // 1. 检测服务心跳异常
    if (!eventInput->service_heartbeat) {
        eventOutput->event_triggered = true;
        eventOutput->event_type = "ServiceFailure";
        eventOutput->severity_level = 3;
        eventOutput->current_phase = 2;
        eventOutput->trigger_signal = 0x01;
        eventOutput->config_id = 1001;
        return eventOutput->return_type;
    }

    // 2. 检测日志中的网络攻击关键词
    std::string lower_log = eventInput->log_message;
    for (char &c : lower_log) c = tolower(c); // 转为小写便于检测
    if (lower_log.find("attack") != std::string::npos || 
        lower_log.find("intrusion") != std::string::npos ||
        lower_log.find("hacker") != std::string::npos) {
        eventOutput->event_triggered = true;
        eventOutput->event_type = "NetworkAttack";
        eventOutput->severity_level = 3;
        eventOutput->current_phase = 1;
        eventOutput->trigger_signal = 0x02;
        eventOutput->config_id = 1002;
        return eventOutput->return_type;
    }

    // 3. 检测内存使用率过高
    if (eventInput->memory_usage > HIGH_MEMORY_THRESHOLD) {
        eventOutput->event_triggered = true;
        eventOutput->event_type = "ServiceFailure";
        eventOutput->severity_level = 3;
        eventOutput->current_phase = 3;
        eventOutput->trigger_signal = 0x03;
        eventOutput->config_id = 1003;
        return eventOutput->return_type;
    }

    // 4. 检测CPU使用率过高
    if (eventInput->cpu_usage > HIGH_CPU_THRESHOLD) {
        eventOutput->event_triggered = true;
        eventOutput->event_type = "ServiceFailure";
        eventOutput->severity_level = 2;
        eventOutput->current_phase = 3;
        eventOutput->trigger_signal = 0x04;
        eventOutput->config_id = 1004;
        return eventOutput->return_type;
    }

    // 5. 检测日志中的数据错误关键词
    if (lower_log.find("data error") != std::string::npos || 
        lower_log.find("corrupt") != std::string::npos ||
        lower_log.find("invalid data") != std::string::npos) {
        eventOutput->event_triggered = true;
        eventOutput->event_type = "DataError";
        eventOutput->severity_level = 2;
        eventOutput->current_phase = 4;
        eventOutput->trigger_signal = 0x05;
        eventOutput->config_id = 1005;
        return eventOutput->return_type;
    }

    // 无事件触发
    return eventOutput->return_type;
}